/**
 * 
 */
package org.swing.utility.jai.bean;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
/**
 * @author Quynh Nhu
 *
 */
public final class Country implements Serializable 
{
	  private static final long serialVersionUID = -2791146125019278973L;

	  public static final Country NULL_COUNTRY = new Country((short)-1, null, null,
	    "(none)");

	  private static List countries;

	  private static Map countryCodeAlpha2Map = new HashMap();

	  private static Map countryNameMap = new HashMap();

	  private short codeNum;

	  private String codeAplha2;

	  private String codeAlpha3;

	  private String name;

	  static {
	    loadCountryCodes();
	  }

	  private Country(final short codeNum, final String codeAplha2,
	    final String codeAlpha3, final String name) {
	    this.codeNum = codeNum;
	    this.codeAplha2 = codeAplha2;
	    this.codeAlpha3 = codeAlpha3;
	    this.name = name;
	  }

	  /**
	   * @return
	   */
	  public String getCodeAlpha3() {
	    return codeAlpha3;
	  }

	  /**
	   * @return
	   */
	  public String getCodeAplha2() {
	    return codeAplha2;
	  }

	  /**
	   * @return
	   */
	  public short getCodeNum() {
	    return codeNum;
	  }

	  /**
	   * @return
	   */
	  public String getName() {
	    return name;
	  }

	  private static void loadCountryCodes() {
	    if (countries == null) {
	      countries = new ArrayList();
	      InputStream in = ClassLoader.getSystemResourceAsStream("org/iso/3166/CountryCodes.txt");
	      if (in != null) {
	        BufferedReader lineReader = new BufferedReader(
	          new InputStreamReader(in));
	        try {
	          String line = lineReader.readLine();
	          for (line = lineReader.readLine(); line != null; line = lineReader.readLine()) {
	            StringTokenizer columns = new StringTokenizer(line, "\t");
	            String alpha2 = columns.nextToken();
	            String alpha3 = columns.nextToken();
	            short num = Short.parseShort(columns.nextToken());
	            String name = columns.nextToken();
	            Country country = new Country(num, alpha2, alpha3, name);
	            countries.add(country);
	            countryCodeAlpha2Map.put(alpha2, country);
	            countryNameMap.put(name.toUpperCase(), country);
	          }
	        } catch (IOException e) {
	        	e.printStackTrace();
	        }
	        
	      }
	    }

	  }

	  public static Country getCountry(final String codeAplha2) {
	    Country country = (Country)countryCodeAlpha2Map.get(codeAplha2.toUpperCase());
	    if (country == null) {
	      throw new IllegalArgumentException("Country for code " + codeAplha2
	        + " does not exist");
	    }
	    return country;
	  }

	  public static Country getCountryByName(final String name) {
	    if (name == null) {
	      return null;
	    }
	    return (Country)countryNameMap.get(name.toUpperCase());
	  }

	  public static List getCountries() {
	    return countries;
	  }

	  public String toString() {
	    return name;
	  }
	}
